import static java.lang.Math.pow;

class Solution {
    public boolean isHappy(int n) {
        int slow = n;
        int fast = fun(n);
        while (slow != fast) {
            slow = fun(slow);
            fast = fun(fun(fast));
        }
        return slow == 1;
    }

    public int fun(int num) {
        int sum = 0;
        while (num > 0) {
            sum += Math.pow(num % 10, 2);
            num /= 10;
        }
        return sum;
    }
}